package com.lingdiman.ocr;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;

import com.nhh.evidenceSdk.http.model.EvidenceZfbResult;
import com.nhh.evidenceSdk.http.model.GjjResult;
import com.nhh.evidenceSdk.http.model.HbCreditValidResult;
import com.nhh.evidenceSdk.http.model.JbCreditValidResult;
import com.nhh.evidenceSdk.http.model.WldResult;
import com.nhh.evidenceSdk.http.model.ZmfValidResult;

public class MainActivity extends AppCompatActivity {

    private TextView zmfTv, gjjTv, wldTv, hbTv;

    private TextView mZfbBtn, mGjjBtn, mWldBtn, mHbBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        zmfTv = findViewById(R.id.zmf_result_tv);
        gjjTv = findViewById(R.id.gjj_result_tv);
        wldTv = findViewById(R.id.wld_result_tv);
        hbTv = findViewById(R.id.hb_result_tv);

        mZfbBtn = findViewById(R.id.zmf_btn);
        mGjjBtn = findViewById(R.id.gjj_btn);
        mWldBtn = findViewById(R.id.wld_btn);
        mHbBtn = findViewById(R.id.hb_btn);


        findViewById(R.id.zmf_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLauncher.launch(FlowDescriptionActivity.getLauncherIntent(MainActivity.this, FlowDescriptionActivity.TYPE_ZFB));
            }
        });
        findViewById(R.id.gjj_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLauncher.launch(FlowDescriptionActivity.getLauncherIntent(MainActivity.this, FlowDescriptionActivity.TYPE_GJJ));
            }
        });

        findViewById(R.id.wld_btn).setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                mLauncher.launch(FlowDescriptionActivity.getLauncherIntent(MainActivity.this, FlowDescriptionActivity.TYPE_WLD));
            }
        });

        mHbBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mLauncher.launch(FlowDescriptionActivity.getLauncherIntent(MainActivity.this, FlowDescriptionActivity.TYPE_WECHAT_HB));
            }
        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private static final String TAG = "MainActivity";

    private ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
        @Override
        public void onActivityResult(ActivityResult result) {
            if (result.getResultCode() == RESULT_OK) {
                Intent data = result.getData();
                setResult(data);
            }
        }
    });

    EvidenceZfbResult mZfbResult;

    private void setResult(Intent data) {
        if (data == null) {
            return;
        }
        String type = data.getStringExtra("type");
        if (FlowDescriptionActivity.TYPE_ZFB.equals(type)) {
            EvidenceZfbResult evidenceZfbResult = (EvidenceZfbResult) data.getSerializableExtra("data");
            if (evidenceZfbResult == null) {
                mZfbBtn.setText("重新认证");
                return;
            }

            ZmfValidResult zmfResult = evidenceZfbResult.getZmfInfo();
            HbCreditValidResult hbResult = evidenceZfbResult.getHbInfo();
            JbCreditValidResult jbResult = evidenceZfbResult.getJbInfo();

            if (TextUtils.isEmpty(evidenceZfbResult.getName()) ||
                    (!TextUtils.isEmpty(evidenceZfbResult.getName()) && zmfResult == null && jbResult == null && hbResult == null)) {
                mZfbBtn.setText("重新认证");
            }

            if (mZfbResult != null && (zmfResult == null || jbResult == null || hbResult == null)) {
                Log.d(TAG, "setResult: 这次的结果：" + evidenceZfbResult.toString());
                evidenceZfbResult = mZfbResult;
                Log.d(TAG, "setResult: 上次的结果----》" + evidenceZfbResult);
            }
            zmfResult = evidenceZfbResult.getZmfInfo();
            jbResult = evidenceZfbResult.getJbInfo();
            hbResult = evidenceZfbResult.getHbInfo();

            boolean zmfSuccess = !TextUtils.isEmpty(evidenceZfbResult.getName()) && zmfResult != null;
//            boolean jbSuccess = !TextUtils.isEmpty(evidenceZfbResult.getName()) && jbResult != null;
            boolean jbSuccess = !TextUtils.isEmpty(evidenceZfbResult.getName()) && (evidenceZfbResult.isJbIsOpen() || jbResult != null);
            boolean hbSuccess = !TextUtils.isEmpty(evidenceZfbResult.getName()) && (evidenceZfbResult.isHbIsOpen() || hbResult != null);

            if (zmfSuccess || jbSuccess || hbSuccess) {
                mZfbBtn.setText("继续认证");
            }

            if (!TextUtils.isEmpty(evidenceZfbResult.getName()) && zmfResult != null
                    && jbResult != null && hbResult != null) {
                mZfbBtn.setText("认证成功");
            }


            String info = getString(R.string.zfb_info_template, evidenceZfbResult.getName(), evidenceZfbResult.getAccount());
            if (zmfResult != null) {
                info = info + getString(R.string.zmf_template, zmfResult.getZhima_score());
            }
            if (evidenceZfbResult.isJbIsOpen() && jbResult != null) {
                info = info + getString(R.string.jb_template, jbResult.getJb_remain_amount(), jbResult.getJb_total_amount(), jbResult.getJb_interest_rate(), jbResult.getIs_overdue());
            } else {
                info = info + "\n借呗未开通\n";
            }
            if (evidenceZfbResult.isHbIsOpen() && hbResult != null) {
                info = info + getString(R.string.hb_template, hbResult.getHb_remain_amount(), hbResult.getHb_total_amount());
            } else {
                info = info + "\n花呗未开通";
            }
            zmfTv.setVisibility(View.VISIBLE);
            zmfTv.setText(info);
            mZfbResult = evidenceZfbResult;
            return;
        }
        if (FlowDescriptionActivity.TYPE_GJJ.equals(type)) {
            GjjResult gjjResult = (GjjResult) data.getSerializableExtra("data");
            if (gjjResult == null) {
                mGjjBtn.setText("重新认证");
                return;
            }
            mGjjBtn.setText("认证成功");

            StringBuilder builder = new StringBuilder();
            builder.append("\n缴存信息：").append("\n");
            for (GjjResult.DetailItem detailItem : gjjResult.getGjjInfo()) {
                builder.append(detailItem.getGjjOcrDate()).append(":  ").append(detailItem.getGjjOcrAmt()).append("\n");
            }
            gjjTv.setVisibility(View.VISIBLE);
            String content = getString(R.string.gjj_template, gjjResult.getGjjOcrName(), gjjResult.getGjjOcrIndDepositBase(),
                    gjjResult.getGjjOcrCoDepositRatio(), gjjResult.getGjjOcrIndDepositRatio(), gjjResult.getGjjOcrCompanyName());

            gjjTv.setText(content + builder.toString());
            return;
        }
        if (FlowDescriptionActivity.TYPE_WLD.equals(type)) {
            WldResult wldResult = (WldResult) data.getSerializableExtra("data");
            if (wldResult == null) {
                mWldBtn.setText("重新认证");
                return;
            }
            if (TextUtils.isEmpty(wldResult.getWldTotalAmt())) {
                mWldBtn.setText("重新认证");
                return;
            }
            mWldBtn.setText("认证成功");
            wldTv.setVisibility(View.VISIBLE);
            wldTv.setText(getString(R.string.wld_template, wldResult.getMobile(), wldResult.getWldRemainAmt(),
                    wldResult.getWldTotalAmt(), wldResult.getWldRate()));
            return;
        }
    }
}